package com.sitech.ibnms.c3p.task.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by liujhc on 12/18.
 */
//@Aspect
//@Component
public class MethodLoggingAspect {
    Logger logger = LoggerFactory.getLogger(MethodLoggingAspect.class);

    @Around("execution(public * com.sitech.ibnms.c3p..*(..))," +
            "execution(public * com.sitech.ismp.workflow..*(..))")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        if (logger.isTraceEnabled()) {
            Class<?> targetClass = joinPoint.getTarget().getClass();
            String targetClassName = targetClass.getName();
            for (Class<?> clazz : targetClass.getClasses()) {
                String className = clazz.getName();
                if (className.startsWith("com.sitech")) {
                    targetClassName = className;
                    break;
                }
            }
            String methodName = joinPoint.getSignature().getName();
            logger.trace("begin:{}.{}()", targetClassName, methodName);
            long begin = System.currentTimeMillis();
            try {
                return joinPoint.proceed();
            } finally {
                logger.trace("end({}ms):{}.{}()", System.currentTimeMillis() - begin, targetClassName, methodName);
            }
        } else {
            return joinPoint.proceed();
        }
    }
}
